package pl.textures
{
	import flash.geom.Point;
	import avmplus.getQualifiedClassName;

	import pl.textures.game.ISingleTexture;
	import pl.textures.game.SingleTexture;
	import pl.textures.packs.TexturePack1Regions;
	import pl.textures.packs.TexturePack2Regions;
	import pl.textures.packs.TexturePacks;

	import starling.textures.Texture;

	import flash.display.Bitmap;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	/**
	 * @author michalr
	 */
	public class Textures
	{
		[Embed(source="assets/grasstile.png")]
		public static const Grass:Class;
		[Embed(source="assets/base.png")]
		public static const Base:Class;
		[Embed(source="assets/baseEnd.png")]
		public static const BaseEnd:Class;
		[Embed(source="assets/cross.png")]
		public static const Cross:Class;
		[Embed(source="assets/cursor.png")]
		public static const Cursor:Class;
		[Embed(source="assets/cursorArrow.png")]
		public static const CursorArrow:Class;
		[Embed(source="assets/cursorDrag.png")]
		public static const CursorDrag:Class;
		[Embed(source="assets/texturepack1.png")]
		public static const TexturePack1:Class;
		[Embed(source="assets/texturepack2.png")]
		public static const TexturePack2:Class;
		[Embed(source="assets/tallTile.png")]
		public static const TallTile:Class;
		[Embed(source="assets/flatSelection.png")]
		public static const FlatSelection:Class;
		//
		private static var gameTextures:Dictionary = new Dictionary();
		private static var gameBitmaps:Dictionary = new Dictionary();
		public static var texturesInitiated:Boolean = false;

		public static function initTexturesCollection():void
		{
			// packs
			gameTextures[TextureID.TP_1] = SingleTexture.factory( TextureID.TP_1, TextureID.TP_1 );
			gameTextures[TextureID.TP_2] = SingleTexture.factory( TextureID.TP_2, TextureID.TP_2 );
			// single
			gameTextures[TextureID.BASE] = SingleTexture.factory( TextureID.BASE, TextureID.BASE );
			gameTextures[TextureID.BASE_END] = SingleTexture.factory( TextureID.BASE_END, TextureID.BASE_END );
			gameTextures[TextureID.GRASS] = SingleTexture.factory( TextureID.GRASS, TextureID.GRASS );
			gameTextures[TextureID.CROSS] = SingleTexture.factory( TextureID.CROSS, TextureID.CROSS );
			gameTextures[TextureID.CURSOR] = SingleTexture.factory( TextureID.CURSOR, TextureID.CURSOR );
			gameTextures[TextureID.CURSOR_ARROW] = SingleTexture.factory( TextureID.CURSOR_ARROW, TextureID.CURSOR_ARROW );
			gameTextures[TextureID.CURSOR_DRAG] = SingleTexture.factory( TextureID.CURSOR_DRAG, TextureID.CURSOR_DRAG );
			gameTextures[TextureID.MUSHROOM] = SingleTexture.factory( TextureID.MUSHROOM, TextureID.MUSHROOM, TexturePacks.PACK1, TexturePack1Regions.MUSHROOM, TexturePivot.MUSHROOM_PIVOT );
			gameTextures[TextureID.SIGN_POST] = SingleTexture.factory( TextureID.SIGN_POST, TextureID.SIGN_POST, TexturePacks.PACK1, TexturePack1Regions.DIRECTION_POST, TexturePivot.SIGNPOST_PIVOT );
			gameTextures[TextureID.BUSH] = SingleTexture.factory( TextureID.BUSH, TextureID.BUSH, TexturePacks.PACK1, TexturePack1Regions.BUSH, TexturePivot.BUSH_PIVOT );
			gameTextures[TextureID.ROCK] = SingleTexture.factory( TextureID.ROCK, TextureID.ROCK, TexturePacks.PACK1, TexturePack1Regions.ROCK, TexturePivot.ROCK_PIVOT );
			gameTextures[TextureID.TREE] = SingleTexture.factory( TextureID.TREE, TextureID.TREE, TexturePacks.PACK1, TexturePack1Regions.TREE, TexturePivot.TREE_PIVOT );
			gameTextures[TextureID.GRASS1_TP2] = SingleTexture.factory( TextureID.GRASS1_TP2, TextureID.GRASS1_TP2, TexturePacks.PACK2, TexturePack2Regions.GRASS1_TP2 );
			gameTextures[TextureID.TALL_TILE] = SingleTexture.factory( TextureID.TALL_TILE, TextureID.TALL_TILE ,0,null,new Point(162,105));
			gameTextures[TextureID.FLAT_SELECTION] = SingleTexture.factory( TextureID.FLAT_SELECTION, TextureID.FLAT_SELECTION);
		}

		public static function getSingleTexture( id:uint ):ISingleTexture
		{
			return gameTextures[id] as ISingleTexture;
		}

		public static function getTextureAndBitmapById( id:uint ):Array
		{
			var bitmap:Bitmap;

			if (id == TextureID.GRASS)
			{
				bitmap = new Textures[getClassName( Grass )]();
				return [getTextureFromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.BASE)
			{
				bitmap = new Textures[getClassName( Base )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.BASE_END)
			{
				bitmap = new Textures[getClassName( BaseEnd )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.CROSS)
			{
				bitmap = new Textures[getClassName( Cross )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.CURSOR)
			{
				bitmap = new Textures[getClassName( Cursor )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.CURSOR_ARROW)
			{
				bitmap = new Textures[getClassName( CursorArrow )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.CURSOR_DRAG)
			{
				bitmap = new Textures[getClassName( CursorDrag )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.TP_1)
			{
				bitmap = new Textures[getClassName( TexturePack1 )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.TP_2)
			{
				bitmap = new Textures[getClassName( TexturePack2 )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.TALL_TILE)
			{
				bitmap = new Textures[getClassName( TallTile )]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}
			if (id == TextureID.FLAT_SELECTION)
			{
				bitmap = new Textures[getClassName( FlatSelection)]();
				return [Texture.fromBitmap( bitmap ), bitmap];
			}

			return null;
		}

		private static function getTextureFromBitmap( bitmap:Bitmap ):Texture
		{
			return Texture.fromBitmap( bitmap );
		}

		private static function getClassName( cl:Class ):String
		{
			return getQualifiedClassName( cl ).split( "_" )[1];
		}

		public static function getTextureAndBitmapFromRegion( texturePack:uint, region:Rectangle ):Array
		{
			if (texturePack == TexturePacks.PACK1)
				return [Texture.fromTexture( (gameTextures[TextureID.TP_1] as SingleTexture).texture, region ), new Bitmap( (gameTextures[TextureID.TP_1] as SingleTexture).bitmapData )];
			if (texturePack == TexturePacks.PACK2)
				return [Texture.fromTexture( (gameTextures[TextureID.TP_2] as SingleTexture).texture, region ), new Bitmap( (gameTextures[TextureID.TP_2] as SingleTexture).bitmapData )];
			return null;
		}
	}
}
